package com.recruit.controller;

import cn.com.webxml.MobileCodeWS;
import cn.com.webxml.MobileCodeWSSoap;
import com.recruit.service.UserService;
import com.recruit.pojo.User;
import com.recruit.utils.Email;
import com.recruit.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/toReg")
    public String toReg() {
        return "before/register";
    }

    @RequestMapping("/login")
    public String login() {
        return "before/user_login";
    }

    @RequestMapping("/toUserInfo")
    public String toUserInfo(HttpSession session,Model model) {
        String userInfo = userService.toUserInfo(session, model);
        return userInfo;
    }

    @RequestMapping("/findCityByParent")
    @ResponseBody
    public ResponseResult findCityByParent(String parent){
        ResponseResult cityByParent = userService.findCityByParent(parent);
        return cityByParent;
    }

    @RequestMapping("/checkTel")
    @ResponseBody
    public ResponseResult checkTel(String tel){
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap port = mobileCodeWS.getPort(MobileCodeWSSoap.class);
        String s = port.getMobileCodeInfo(tel, null);
        if (s.contains("手机号码错误")){
            return new ResponseResult(201,"手机号码错误");
        }
        return new ResponseResult(200,"手机号码正确");
    }

    @RequestMapping("/sendEmail")
    @ResponseBody
    public ResponseResult sendEmail(String email, HttpSession session) {
        cn.com.webxml.ValidateEmailWebService service = new cn.com.webxml.ValidateEmailWebService();
        cn.com.webxml.ValidateEmailWebServiceSoap port = service.getPort(cn.com.webxml.ValidateEmailWebServiceSoap.class);
        short i = port.validateEmailAddress(email);
        if (i!=1){
            return new ResponseResult(201, "请输入正确的邮件");
        }
        String result = Email.sendEmail(email);
        if (result == null) {
            return new ResponseResult(202, "发送失败");
        }
        session.setAttribute("code", result);
        return new ResponseResult(200, "发送成功", result);
    }

    @RequestMapping("/doReg")
    @ResponseBody
    public ResponseResult doReg(User user, String code, HttpSession session) {
        System.out.println(user);
        ResponseResult result = userService.doReg(user, code, session);
        return result;
    }

    @RequestMapping("/doLogin")
    @ResponseBody
    public ResponseResult doLogin(User user, HttpSession session) {
        ResponseResult result = userService.doLogin(user, session);
        return result;
    }

    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public ResponseResult updateUserInfo(User user){
        System.out.println(user);
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap port = mobileCodeWS.getPort(MobileCodeWSSoap.class);
        String s = port.getMobileCodeInfo(user.getTel(), null);
        if (s.contains("手机号码错误")){
            return new ResponseResult(201,"手机号码错误");
        }
        ResponseResult result = userService.updataUserInfo(user);
        System.out.println(result);
        return result;
    }
}
